#include "groupsessiondetailinfo.h"
#include "singlesessiondetailinfo.h"
#include <QPushButton>
#include <QScrollArea>
#include <QScrollBar>
#include "debug.h"


GroupSessionDetailInfo::GroupSessionDetailInfo(QWidget* parent,const model::SessionInfo& sessionInfo)
:QDialog(parent)
{
    //1.设置属性
    this->setWindowTitle("会话详情");
    this->setFixedSize(410,500);
    this->setAttribute(Qt::WA_DeleteOnClose);

    //2.设置布局管理器
    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->setSpacing(10);
    layout->setContentsMargins(20,20,20,20);

    //3.向布局管理器添加控件


        //1.添加滚动区域
    QScrollArea* scrollArea = new QScrollArea;
    scrollArea->setWidgetResizable(true);
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal{height:0px;}");
    scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical{width:3px;background-color:rgb(216,216,216);}");
    layout->addWidget(scrollArea);

    QWidget* w =new QWidget;
    scrollArea->setWidget(w);
    gLayout = new QGridLayout(w);


    AvatarItem* addItem = new AvatarItem(QIcon(":/myResource/Image/addMember.svg"),"添加");
    this->addMember(addItem);

        //2.添加"群聊名称"标签
    QLabel* groupNameTag = new QLabel;
    groupNameTag->setText("群聊名称:");
    QFont font("微软雅黑",12,30);
    groupNameTag->setFont(font);
    layout->addWidget(groupNameTag);

        //3.添加群聊名称和修改按钮
    QHBoxLayout* hLayout = new QHBoxLayout;
    layout->addLayout(hLayout);

    groupName = new QLabel;
    QFont font2("微软雅黑",10,20);
    groupName->setFont(font2);
    QPushButton* groupNameChange = new QPushButton;
    groupNameChange->setIcon(QIcon(":/myResource/Image/change.svg"));
    groupNameChange->setFixedSize(30,30);
    groupNameChange->setIconSize(QSize(30,30));
    hLayout->addWidget(groupName);
    hLayout->addWidget(groupNameChange);

        //添加删除好友按钮
    QPushButton* deleteBtn = new QPushButton;
    deleteBtn->setText("退出群聊");
    deleteBtn->setFixedHeight(50);
    QString style ="QPushButton{border:1px solid black;border-radius:5px;background-color:white;}";
    style+="QPushButton:pressed{background-color:rgb(210,210,210);}";
    deleteBtn->setStyleSheet(style);
    layout->addWidget(deleteBtn);

    model::DataCenter* dataCenter =model::DataCenter::getInstance();
    groupName->setText(sessionInfo.chatSessionName);
    if(dataCenter->getSessionMemberList(sessionInfo.chatSessionId))
        initMembers(sessionInfo.chatSessionId);
    else
    {
        connect(dataCenter,&model::DataCenter::getSessionMemberListDone,this,&GroupSessionDetailInfo::initMembers);
        dataCenter->getSessionMemberListAsync(sessionInfo.chatSessionId);
    }

#if TEST_UI
    //测试数据
    for(int i =0;i<20;i++)
    {
        AvatarItem* MemberItem = new AvatarItem(QIcon(":/myResource/Image/dogAvatar.svg"),"dog");
        this->addMember(MemberItem);
    }
    groupName->setText("zzy的群聊");
#endif

};
void GroupSessionDetailInfo::addMember(AvatarItem* item)
{
    const int MAX_COL = 4;
    int row = itemNum/MAX_COL;
    int col = itemNum%MAX_COL;
    gLayout->addWidget(item,row,col);
    itemNum++;
}

void GroupSessionDetailInfo::initMembers(const QString& sessionId)
{
    model::DataCenter* dataCenter =model::DataCenter::getInstance();
    QList<model::UserInfo> *userList=    dataCenter->getSessionMemberList(sessionId);
    for(auto u:*userList)
        addMember(new AvatarItem(u.avatar,u.nickname));
}
